package com.css.bjdt_report.executive.report;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * 一个批次请求对应一个会话(暂时没提供remove、clear方法)
 */
public class ReportSession {

	private ConcurrentMap<String, Map<String, Object>> cache = new ConcurrentHashMap<String, Map<String, Object>>();

	public static String CELL_CACHE = "cellCache";
	public static String CELLDATA_CACHE = "cellDataCache";
	public static String REPORT_CACHE = "reportCache";

	public ReportSession() {
		//预先初始化3块缓存
		ConcurrentMap<String, Object> cellCache = new ConcurrentHashMap<String, Object>();
		ConcurrentMap<String, Object> cellDataCache = new ConcurrentHashMap<String, Object>();
		ConcurrentMap<String, Object> reportCache = new ConcurrentHashMap<String, Object>();
		cache.put(CELL_CACHE, cellCache);
		cache.put(CELLDATA_CACHE, cellDataCache);
		cache.put(REPORT_CACHE, reportCache);
	}

	public Object getCell(String key) {
		return get(CELL_CACHE, key);
	}
	
	public void putCell(String key, Object value) {
		put(CELL_CACHE, key, value);
	}
	
	public Object getCellData(String key) {
		return get(CELLDATA_CACHE, key);
	}
	
	public void putCellData(String key, Object value) {
		put(CELLDATA_CACHE, key, value);
	}

	public Object get(String cacheName, String key) {
		if (cache.containsKey(cacheName)) {
			if (cache.get(cacheName).containsKey(key)) {
				return cache.get(cacheName).get(key);
			}
		}
		return null;
	}
	
	public void put(String cacheName, String key, Object value) {
		if(!cache.containsKey(cacheName)){
			Map<String, Object> map = new HashMap<String, Object>();
			cache.put(key, map);
		}
		cache.get(cacheName).put(key, value);
	}
	
	public Map<String, Object> getCache(String cacheName) {
		if(cache.containsKey(cacheName)){
			return cache.get(cacheName);
		}
		return null;
	}
}
